//
//  BIDListViewII.m
//  EducationAssistant
//
//  Created by mal on 15/8/27.
//  Copyright (c) 2015年 cnsoft. All rights reserved.
//

#import "BIDListViewII.h"
#import "BIDPullDownRefreshView.h"
#import "BIDLoadMoreDataView.h"

static const NSUInteger MIN_PULL_DOWN_DISTANCE = 50;

@interface BIDListViewII()<UITableViewDataSource, UITableViewDelegate>
{
    BIDPullDownRefreshView *_pullDownRefreshView;
    BIDLoadMoreDataView *_loadMoreDataView;
    //下拉刷新
    BOOL _bPullDown;
    //上拉加载更多
    BOOL _bPullUp;
    //是否可以刷新
    BOOL _bCanRefresh;
    //是否还有数据
    BOOL _bHasMoreData;
    //加载或刷新中
    BOOL _bLoading;
    BOOL _bDragging;
}
@end

@implementation BIDListViewII
@synthesize myDelegate;

- (void)awakeFromNib
{
    [self initForItselfWithFrame:self.frame];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self initForItselfWithFrame:frame];
    }
    return self;
}

- (void)initForItselfWithFrame:(CGRect)frame
{
    self.dataSource = self;
    self.delegate = self;
    _bHasMoreData = YES;
    //
    //
    _loadMoreDataView = (BIDLoadMoreDataView*)[[[NSBundle mainBundle] loadNibNamed:@"BIDLoadMoreDataView" owner:self options:nil] lastObject];
    CGRect loadMoreDataViewFrame = _loadMoreDataView.frame;
    loadMoreDataViewFrame.origin.x = 0;
    loadMoreDataViewFrame.origin.y = -loadMoreDataViewFrame.size.height;
    loadMoreDataViewFrame.size.width = frame.size.width;
    _loadMoreDataView.frame = loadMoreDataViewFrame;
    _loadMoreDataView.hidden = NO;
    [self addSubview:_loadMoreDataView];
    //
    UIView *footerView = [[UIView alloc] init];
    self.tableFooterView = footerView;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if(self)
    {}
    return self;
}

- (void)clearData
{
}

- (void)removeRefreshView
{
}

/**
 *刷新结束
 */
- (void)endRefresh
{
    //[self setBackgroundColor:[UIColor whiteColor]];
    _bLoading = NO;
    [_loadMoreDataView taskEnd];
    [self setContentInset:UIEdgeInsetsZero];
}
/**
 *  没有更多数据了
 */
- (void)hasNoMoreData
{
    _bHasMoreData = NO;
    [_loadMoreDataView hasNoMoreData];
}

- (void)setRefreshViewBgColor:(UIColor *)bgColor
{
    _loadMoreDataView.backgroundColor = bgColor;
}

#pragma mark -UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    if(offset.y<0)
    {
        //下拉
        if(!_bLoading && _bHasMoreData)
        {
            if(fabsf(offset.y) > MIN_PULL_DOWN_DISTANCE)
            {
                [_loadMoreDataView taskReady];
            }
            else
            {
                [_loadMoreDataView taskIdle];
            }
        }
    }
    else
    {
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGPoint offset = scrollView.contentOffset;
    if(offset.y<0)
    {
        //下拉加载更多
        if(!_bLoading && fabsf(offset.y) > MIN_PULL_DOWN_DISTANCE && _bHasMoreData)
        {
            [scrollView setContentInset:UIEdgeInsetsMake(CGRectGetHeight(_loadMoreDataView.frame), 0, 0, 0)];
            _bLoading = YES;
            //可以刷新并且当前不是正在刷新中这个状态
            [_loadMoreDataView taskStart];
            [myDelegate loadMoreData];
        }
    }
    else
    {
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([myDelegate respondsToSelector:@selector(numberOfSectionsInTableView:)])
    {
        return [myDelegate numberOfSectionsInTableView:tableView];
    }
    else
    {
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows = [myDelegate tableView:tableView numberOfRowsInSection:section];
    return rows;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [myDelegate tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.f;
    height = [myDelegate tableView:tableView heightForRowAtIndexPath:indexPath];
    return height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [myDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if([myDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)])
    {
        return [myDelegate tableView:tableView heightForHeaderInSection:section];
    }
    else
    {
        return 0;
    }
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if([myDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)])
    {
        return [myDelegate tableView:tableView viewForHeaderInSection:section];
    }
    else
    {
        return nil;
    }
}

@end
